package com.zoweunion.mechanic.plugins.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitmqConfig {
    /**
     * 实时通讯报文队列，推送前端
     */
    public final static String QUEUE_REAL_MESSAGE = "real_message";
    /**
     * 实时转发通讯报文队列
     */
    public final static String QUEUE_REAL_ORIGINAL_MESSAGE = "real_original_message";

    /**
     * 新协议实时转发通讯报文队列
     */
    public final static String NEW_QUEUE_REAL_DATA = "new_real_data";


    /**
     * 转到APP-PC后台做提醒报警的队列
     */
    public final static String QUEUE_REAL_REMIND_ALARM_MESSAGE = "real_remind_alarm_message";


    /**
     * 转到log-server后台做提醒报警的队列
     */
    public final static String LOG_SERVER_MESSAGE = "log_server_message";


    /**
     *
     */
    public final static String QUEUE_REAL_DATA = "real_data";

    /**
     * 旧协议处理历史数据
     */
    public final static String QUEUE_CHANEL_MESSAGE = "chanel_message";
    /**
     * 实时通讯报文队列
     */
    public final static String NEW_QUEUE_CHANEL_MESSAGE = "new_chanel_message";
    /**
     * 预警数量队列
     */
    public final static String TOPIC_EARLY_WARN = "early-warn";
    /**
     * 车辆在线数量队列
     */
    public final static String TOPIC_CAR_ONLINE = "car-onLine";
    /**
     * 车辆在线数量队列
     */
    public final static String TOPIC_GATEWAY_ONLINE = "gateway-onLine";
    /**
     * 插入业务数据库的数量队列
     */
    public final static String TOPIC_MYSQL_COUNT = "mysql-count";
    /**
     * redis数据库的键数量队列
     */
    public final static String TOPIC_REDISKEY_COUNT = "redisKey-count";
    /**
     * 内存占用率队列
     */
    public final static String TOPIC_MEMORY_OCCUPY = "memory-occupy";
    /**
     * cpu使用率队列
     */
    public final static String TOPIC_CPU_OCCUPY = "cpu-occupy";


    /**
     * topic 交换机
     * 路由模式
     */
    public final static String IOT_SERVER_TOPIC_EXCHANGE = "iot_server.topic_exchange";

    /**
     * direct 交换机
     * 完全匹配单播模式
     */
    public final static String IOT_SERVER_DIRECT_EXCHANGE = "iot_server.direct_exchange";
    /**
     * fanout 交换机
     * 广播模式
     */
    public final static String IOT_SERVER_FANOUT_EXCHANGE = "iot_server.fanout_exchange";


    public final static String ROUTE_KEY_REAL_MESSAGE = "route_key_real_message";

    public final static String ROUTE_KEY_REAL_ORIGINAL_MESSAGE = "route_key_real_original_message";

    public final static String ROUTE_KEY_REAL_DATA = "route_key_real_data";

    public final static String NEW_ROUTE_KEY_REAL_DATA = "new_route_key_real_data";

    public final static String ROUTE_KEY_CHANEL_MESSAGE = "route_key_chanel_message";

    public final static String NEW_ROUTE_KEY_CHANEL_MESSAGE = "new_route_key_chanel_message";

    public final static String ROUTE_KEY_REAL_REMIND_ALARM_MESSAGE = "route_key_real_remind_alarm_message";

    public final static String ROUTE_KEY_LOG_SERVER_MESSAGE = "route_log_server_message";

    public final static String ROUTE_KEY_EARLY_WARN = "route_key_early_warn";

    public final static String ROUTE_KEY_CAR_ONLINE = "route_key_car_online";

    public final static String ROUTE_KEY_GATEWAY_ONLINE = "route_key_gateway_online";

    public final static String ROUTE_KEY_MYSQL_COUNT = "route_key_mysql_count";

    public final static String ROUTE_KEY_REDISKEY_COUNT = "route_key_redisKey_count";

    public final static String ROUTE_KEY_MEMORY_OCCUPY = "route_key_memory_occupy";

    public final static String ROUTE_KEY_CPU_OCCUPY = "route_key_cpu_occupy";


    /**
     * 实时转发通讯报文队列
     * @return
     */
    @Bean
    public Queue realOriginalMessageQueue() {
        HashMap<String, Object> arguments = new HashMap<>(5);
        // 设置消息存活时间
        arguments.put("x-message-ttl", 1000 * 60 * 30);
        return new Queue(QUEUE_REAL_ORIGINAL_MESSAGE, true, false, false, arguments);
    }

    /**
     * 绑定message交换机和队列，包括路由key映射关系   实时转发通讯报文队列
     */
    @Bean
    public Binding bindOriginalMessageQueueExchange() {
        return BindingBuilder.bind(realOriginalMessageQueue()).to(topicExchange()).with(ROUTE_KEY_REAL_ORIGINAL_MESSAGE);
    }

    /**
     * 旧协议处理历史数据
     * @return
     */
    @Bean
    public Queue channelMessageQueue() {
        HashMap<String, Object> arguments = new HashMap<>(5);
        // 设置消息存活时间
        arguments.put("x-message-ttl", 1000 * 60 * 30);
        return new Queue(QUEUE_CHANEL_MESSAGE, true, false, false, arguments);
    }

    /**
     * 绑定data交换机和队列，包括路由key映射关系
     */
    @Bean
    public Binding bindChanelMessageTopicExchange() {
        return BindingBuilder.bind(channelMessageQueue()).to(topicExchange()).with(ROUTE_KEY_CHANEL_MESSAGE);
    }

    /**
     * pc-app报警的通道
     * @return
     */
    @Bean
    public Queue channelremindAlarmMessageQueue() {
        HashMap<String, Object> arguments = new HashMap<>(5);
        // 设置消息存活时间
        arguments.put("x-message-ttl", 1000 * 60 * 30);
        return new Queue(QUEUE_REAL_REMIND_ALARM_MESSAGE, true, false, false, arguments);
    }

    /**
     * 绑定data交换机和队列，包括路由key映射关系  LOG_SERVER_MESSAGE
     */
    @Bean
    public Binding bindRemindAlarmChanelMessageTopicExchange() {
        return BindingBuilder.bind(channelremindAlarmMessageQueue()).to(topicExchange()).with(ROUTE_KEY_REAL_REMIND_ALARM_MESSAGE);
    }



    /**
     * log-server日志的通道
     * @return
     */
    @Bean
    public Queue logServerMessageQueue() {
        HashMap<String, Object> arguments = new HashMap<>(5);
        // 设置消息存活时间
        arguments.put("x-message-ttl", 1000 * 60 * 30);
        return new Queue(LOG_SERVER_MESSAGE, true, false, false, arguments);
    }

    /**
     * 绑定data交换机和队列，包括路由key映射关系  LOG_SERVER_MESSAGE
     */
    @Bean
    public Binding bindLogServerMessageTopicExchange() {
        return BindingBuilder.bind(logServerMessageQueue()).to(topicExchange()).with(ROUTE_KEY_LOG_SERVER_MESSAGE);
    }

    /**
     * 新的协议---实时数据
     * @return
     */
    @Bean
    public Queue newRealDataMessageQueue() {
        HashMap<String, Object> arguments = new HashMap<>(5);
        // 设置消息存活时间
        arguments.put("x-message-ttl", 1000 * 60 * 30);
        return new Queue(NEW_QUEUE_REAL_DATA, true, false, false, arguments);
    }

    /**
     * 绑定data交换机和队列，包括路由key映射关系
     */
    @Bean
    public Binding bindNewRealDataMessageTopicExchange() {
        return BindingBuilder.bind(newRealDataMessageQueue()).to(topicExchange()).with(NEW_ROUTE_KEY_REAL_DATA);
    }

    /**
     * 新协议---历史数据
     * @return
     */
    @Bean
    public Queue newChannelMessageQueue() {
        HashMap<String, Object> arguments = new HashMap<>(5);
        // 设置消息存活时间
        arguments.put("x-message-ttl", 1000 * 60 * 30);
        return new Queue(NEW_QUEUE_CHANEL_MESSAGE, true, false, false, arguments);
    }

    /**
     * 绑定data交换机和队列，包括路由key映射关系
     */
    @Bean
    public Binding bindNewChanelMessageTopicExchange() {
        return BindingBuilder.bind(newChannelMessageQueue()).to(topicExchange()).with(NEW_ROUTE_KEY_CHANEL_MESSAGE);
    }

    /**
     * 声明交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(IOT_SERVER_TOPIC_EXCHANGE, true, false);
    }
    @Bean
    public DirectExchange directExchangeExchange() {
        return new DirectExchange(IOT_SERVER_DIRECT_EXCHANGE, true, false);
    }
    @Bean
    public DirectExchange fanoutExchangeExchange() {
        return new DirectExchange(IOT_SERVER_FANOUT_EXCHANGE, true, false);
    }










}
